package com.hspedu.newData.dmsxl.哈希表;

import org.junit.Test;

import java.util.HashSet;

/**
 * @Author: yh
 * @description: 
 * @CreateTime: 2025-05-27 
 * @Version: 1.0
 */

// todo
public class LC51缺失的第一个正整数 {

    public int firstMissingPositive(int[] nums) {

        int j = 0;
        HashSet<Integer> set = new HashSet<>();
        for (int i = 0; i < nums.length; i++) {
            set.add(nums[i]);
            if (nums[i]>0 && nums[i]>j){
                j = nums[i];
            }
        }
        int res = -1;
        if (j == 2147483647){
            for (int i = 1; i <= j; i++) {
                if (!set.contains(i)){
                    res = i;
                    break;
                }
            }
            return res;
        }
        for (int i = 1; i <= j+1; i++) {
            if (!set.contains(i)){
                res = i;
                break;
            }
        }
        return res;
    }

    @Test
    public void test() {
        // 测试代码

        int i = firstMissingPositive(new int[]{2147483647});
        int j = firstMissingPositive(new int[]{1,2,3,10,2147483647,9});
        int k = firstMissingPositive(new int[]{7,8,9,11,12});

        System.out.println("111");
    }
}
